home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dd / umain.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  3KB  |  115 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6. /*
  7.  *  _MAIN.C
  8.  *
  9.  *  Note that to use this _main we must be a process.  The programmer
  10.  *  can overide our _main with his own if he wishes a task entry.
  11.  *
  12.  *  Note that we open '*' with modes 1005 so if the filesystem is our stderr
  13.  *  we don't create a file called '*'.
  14.  */
  15. #include    <localdefs.h>
  16.  
  17. extern int    wbmain(void *);
  18. extern int    main(int, char **);
  19. extern void    _waitwbmsg(void);
  20.  
  21. extern WBMSG    *_WBMsg;
  22. extern EBASE    *SysBase;
  23.  
  24. char        *args;
  25. ULONG        argSize;
  26. TASK        *thisTask;
  27. APROCESS    *thisProcess;
  28. CLI        *thisCli;
  29. APTR        debugStack, debugStackTop, systemTrapHandler;
  30. BPTR        commandNameSave;
  31.  
  32.  
  33. __stkargs void _main(short len, char *arg) {
  34.     APROCESS *proc = (APROCESS *)FindTask(NULL);
  35.  
  36. #ifdef NOTDEF    /* added by matt to debug the debugger */
  37.     arg = "hello a b c\r\n";
  38.     len = 13;
  39. #endif
  40.  
  41.     thisProcess = proc; thisTask = (TASK *)proc;
  42.     systemTrapHandler = thisTask->tc_TrapCode;
  43.     debugStack = thisTask->tc_SPLower; debugStackTop = thisTask->tc_SPUpper;
  44.     thisCli = BTOC(proc->pr_CLI, CLI);
  45.     commandNameSave = thisCli->cli_CommandName;
  46.     args = arg; argSize = len;
  47.  
  48.     if (proc->pr_Task.tc_Node.ln_Type == NT_PROCESS) {
  49.         /*DOSBase = OpenLibrary("dos.library", 0);*/
  50.  
  51.         if (proc->pr_CIS) {
  52.             _IoStaticFD[0].fd_Fh = proc->pr_CIS;
  53.             _IoStaticFD[0].fd_Flags = O_RDWR | O_NOCLOSE | O_ISOPEN;
  54.         }
  55.  
  56.         if (proc->pr_COS) {
  57.             _IoStaticFD[1].fd_Fh = proc->pr_COS;
  58.             _IoStaticFD[1].fd_Flags = O_RDWR | O_NOCLOSE | O_ISOPEN;
  59.         }
  60.  
  61.         if (proc->pr_ConsoleTask && proc->pr_CLI) {
  62.             CLI *cli = BTOC(proc->pr_CLI, CLI);
  63.  
  64.             if (cli->cli_Background == 0) {
  65.             if (_IoStaticFD[2].fd_Fh = Open("*", 1005))
  66.                 _IoStaticFD[2].fd_Flags = O_RDWR | O_ISOPEN;
  67.             }
  68.             if (_IoStaticFD[2].fd_Fh == NULL && _IoStaticFD[1].fd_Fh) {
  69.             _IoStaticFD[2].fd_Fh = _IoStaticFD[1].fd_Fh;
  70.             _IoStaticFD[2].fd_Flags = O_RDWR | O_NOCLOSE | O_ISOPEN;
  71.             }
  72.         }
  73.     }
  74.  
  75.     _finitdesc(stdin,  0, __SIF_READ  | __SIF_NOFREE);
  76.     _finitdesc(stdout, 1, __SIF_WRITE | __SIF_NOFREE);
  77.     _finitdesc(stderr, 2, __SIF_WRITE | __SIF_NOFREE);
  78.  
  79.     if (proc->pr_Task.tc_Node.ln_Type == NT_PROCESS) {
  80.         CLI *cli;
  81.  
  82.         if (cli = BTOC(proc->pr_CLI, CLI)) {
  83.             char **av;
  84.             int ac;
  85.             unsigned char *copy = malloc(len+1);
  86.             unsigned char *cname = (unsigned char *)((long)cli->cli_CommandName << 2);
  87.  
  88.             _slow_bcopy(arg, copy, len);
  89.             copy[len] = 0;
  90.             ac = _parseargs1(copy, len);
  91.             av = malloc((ac + 2) << 2);
  92.             _parseargs2(copy, av+1, ac);
  93.  
  94.             ++ac;    /* include arg0 */
  95.  
  96.             if (cname) {
  97.             len = *cname;
  98.  
  99.             arg = malloc(len+1);
  100.             _slow_bcopy(cname + 1, arg, len);
  101.             arg[len] = 0;
  102.             }
  103.             av[0] = arg;
  104.             av[ac] = 0;
  105.             exit(main(ac, av));
  106.         } else {
  107.             CurrentDir(_WBMsg->sm_ArgList->wa_Lock);
  108.             exit(wbmain(_WBMsg));
  109.         }
  110.     }
  111.     exit(-1);
  112.     _waitwbmsg();    /*  dummy, brings in alib/wbmain.a    */
  113. }
  114.  
  115.